package com.majsubtense.main.ui;

import com.majsubtense.core.components.ComponentType;
import com.majsubtense.core.components.Render;
import com.majsubtense.core.components.UI;

import com.majsubtense.core.events.GameControlEvent;
import com.majsubtense.core.events.GameSceneChangeEvent;
import com.majsubtense.core.events.GameSceneControlEvent;
import javafx.animation.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.util.Duration;

public class RaceVenueUI extends UI {
    public static final ComponentType<RaceVenueUI> ANY;
    private Button btn_toTitle;
    private Button btn_reset;

    public RaceVenueUI() {
        super(RaceVenueUI.ANY);
        pane = new AnchorPane();
        scene = new Scene(pane);

        btn_toTitle = new Button("Title");
        btn_toTitle.setLayoutX(517.0f);
        btn_toTitle.setLayoutY(19.0f);
        btn_toTitle.setPrefWidth(58.0f);
        btn_toTitle.setPrefHeight(23.0f);
        btn_toTitle.setOnMousePressed(mouseEvent ->
                onDeliver(new GameSceneChangeEvent(GameSceneChangeEvent.ANY, "RaceVenue", "Index"))
        );

        btn_reset = new Button("Reset");
        btn_reset.setLayoutX(517.0f);
        btn_reset.setLayoutY(47.0f);
        btn_reset.setPrefWidth(58.0f);
        btn_reset.setPrefHeight(23.0f);
        btn_reset.setOnMousePressed(mouseEvent ->
                onDeliver(new GameSceneControlEvent(GameSceneControlEvent.GAME_SCENE_RESET))
        );
    }

    @Override
    public void attach() {
        Canvas canvas = getParentContainer().getComponent(Render.ANY).getCanvas();
        canvas.setLayoutX(0);
        canvas.setLayoutY(0);
        canvas.setWidth(600);
        canvas.setHeight(400);

        pane.getChildren().addAll(canvas, btn_toTitle, btn_reset);
    }

    @Override
    public void update() {
    }

    @Override
    public void detach() {
        Canvas canvas = getParentContainer().getComponent(Render.ANY).getCanvas();

        pane.getChildren().removeAll(canvas, btn_toTitle, btn_reset);
    }

    @Override
    public void showEffect(String text, float[] position) {
        Label label = new Label(text);
        label.setLayoutX(position[0]);
        label.setLayoutY(position[1] + 20);
        label.setFont(Font.font("Arial", FontWeight.BOLD, 15));
        label.setWrapText(true);

        Path path = new Path();
        path.getElements().add(new MoveTo(0, 0));
        path.getElements().add(new LineTo(0, -80f));

        FadeTransition fadeTransition = new FadeTransition(Duration.seconds(3), label);
        fadeTransition.setFromValue(1.0);
        fadeTransition.setToValue(0.0);

        PathTransition pathTransition = new PathTransition(Duration.seconds(3), path, label);

        pathTransition.play();
        fadeTransition.play();
        pathTransition.setOnFinished(event -> {
            pane.getChildren().remove(label);
        });

        pane.getChildren().add(label);
    }

    public void startScene() {
        Label label_3 = new Label("3");
        label_3.setFont(Font.font("Arial", FontWeight.BOLD, 80));
        Label label_2 = new Label("2");
        label_2.setFont(Font.font("Arial", FontWeight.BOLD, 80));
        Label label_1 = new Label("1");
        label_1.setFont(Font.font("Arial", FontWeight.BOLD, 80));
        Label label_GO = new Label("GO!");
        label_GO.setFont(Font.font("Arial", FontWeight.BOLD, 80));

        label_3.setOpacity(0);
        label_3.setLayoutX(285);
        label_3.setLayoutY(200);
        label_2.setOpacity(0);
        label_2.setLayoutX(285);
        label_2.setLayoutY(200);
        label_1.setOpacity(0);
        label_1.setLayoutX(285);
        label_1.setLayoutY(200);
        label_GO.setOpacity(0);
        label_GO.setLayoutX(230);
        label_GO.setLayoutY(200);

        Timeline timeline = new Timeline();
        timeline.getKeyFrames().addAll(
                new KeyFrame(Duration.ZERO,
                        new KeyValue(label_3.opacityProperty(), 0),
                        new KeyValue(label_3.layoutYProperty(), 200)),
                new KeyFrame(Duration.seconds(0.33),
                        new KeyValue(label_3.opacityProperty(), 1),
                        new KeyValue(label_3.layoutYProperty(), 150)
                ),
                new KeyFrame(Duration.seconds(0.67),
                        new KeyValue(label_3.opacityProperty(), 0),
                        new KeyValue(label_3.layoutYProperty(), 100)
                ),
                new KeyFrame(Duration.seconds(1),
                        new KeyValue(label_2.opacityProperty(), 0),
                        new KeyValue(label_2.layoutYProperty(), 200)),
                new KeyFrame(Duration.seconds(1.33),
                        new KeyValue(label_2.opacityProperty(), 1),
                        new KeyValue(label_2.layoutYProperty(), 150)
                ),
                new KeyFrame(Duration.seconds(1.67),
                        new KeyValue(label_2.opacityProperty(), 0),
                        new KeyValue(label_2.layoutYProperty(), 100)
                ),
                new KeyFrame(Duration.seconds(2),
                        new KeyValue(label_1.opacityProperty(), 0),
                        new KeyValue(label_1.layoutYProperty(), 200)),
                new KeyFrame(Duration.seconds(2.33),
                        new KeyValue(label_1.opacityProperty(), 1),
                        new KeyValue(label_1.layoutYProperty(), 150)
                ),
                new KeyFrame(Duration.seconds(2.67),
                        new KeyValue(label_1.opacityProperty(), 0),
                        new KeyValue(label_1.layoutYProperty(), 100)
                ),
                new KeyFrame(Duration.seconds(3),
                        new KeyValue(label_GO.opacityProperty(), 0),
                        new KeyValue(label_GO.layoutYProperty(), 200)),
                new KeyFrame(Duration.seconds(3.33),
                        new KeyValue(label_GO.opacityProperty(), 1),
                        new KeyValue(label_GO.layoutYProperty(), 150)
                ),
                new KeyFrame(Duration.seconds(3.67),
                        new KeyValue(label_GO.opacityProperty(), 0),
                        new KeyValue(label_GO.layoutYProperty(), 100)
                )
        );

        pane.getChildren().addAll(label_3, label_2, label_1, label_GO);

        timeline.play();
        timeline.setOnFinished(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                pane.getChildren().removeAll(label_3, label_2, label_1, label_GO);
                onDeliver(new GameSceneControlEvent(GameSceneControlEvent.GAME_SCENE_START));
            }
        });
    }

    static {
        ANY = new ComponentType<>(UI.ANY, "RACE_VENUE");
    }
}
